package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.PointMsg;
import cn.wolfcode.trip.base.domain.SignIn;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.PointMsgMapper;
import cn.wolfcode.trip.base.mapper.SignInMapper;
import cn.wolfcode.trip.base.mapper.UserMapper;
import cn.wolfcode.trip.base.service.ISignInService;
import cn.wolfcode.trip.base.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class SignInServiceImpl implements ISignInService {
    @Autowired
    private SignInMapper signInMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private PointMsgMapper pointMsgMapper;

    @Override
    public List<Date> listSignInTimeById(Long id) {
        SignIn sign = signInMapper.selectByPrimaryKey(id);
        if (sign==null){
            return null;
        }
        Date begin = sign.getBeginTime();//开始签到时间
        Date lastTime = sign.getLastTime();//最新更新时间
        Integer count = sign.getSigninCount();//累计签到次数
        List<Date> all =new ArrayList<>();
        //如果没有连续签到，返回空签到记录
        if (!lastTime.equals(DateUtil.getAddDay(begin, count-1))){
            return null;
        }

        if (DateUtil.isCurrentMonth(lastTime)&&count!=null){
            //获取用户当月所有的签到时间
            for (Integer i = 0; i < count; i++) {
                all.add(DateUtil.getAddDay(lastTime, -i));
            }
        }else {
            //最新签到时间不是当月时间，返回空签到记录
            return null;
        }

        return all;
    }

    @Override
    public void saveOrUpdate(SignIn signIn) {
        //签到次数
        Integer count = signIn.getSigninCount();
        //添加积分明细
        PointMsg msg = new PointMsg();

        User user = new User();
        user.setId(signIn.getId());

        msg.setUser(user);
        msg.setCreateTime(new Date());

        if(count==null){
            return;
        }

        signIn.setLastTime(new Date());

        if (signIn.getId()!=null){
            if(count>1){
                //更新签到次数
                signInMapper.update(signIn);
                //根据签到次数添加积分
                if (count<=7){
                    userMapper.addPoint(signIn.getId(),count);
                    msg.setMsg("签到成功,获得"+count+"积分");
                }
                if(count%3==0){
                    if(count==30){
                        //签到30天+100
                        userMapper.addPoint(signIn.getId(),100);
                        msg.setMsg("连续签到30天,获得100积分");
                        return;
                    }
                    //连续签到三天奖励10积分
                    userMapper.addPoint(signIn.getId(),10);
                    msg.setMsg("连续签到3天,获得10积分");
                }

                if(count==20){
                    //签到20天+50
                    userMapper.addPoint(signIn.getId(),50);
                    msg.setMsg("连续签到20天,获得50积分");
                }

            }else {
                signInMapper.delete(signIn.getId());
                signIn.setBeginTime(new Date());
                //保存签到记录
                signInMapper.insert(signIn);
                //添加一个积分
                userMapper.addPoint(signIn.getId(),1);


                msg.setMsg("签到成功,获得1积分");
            }
        }
        //添加积分明细记录
        pointMsgMapper.insert(msg);
    }
}
